package org.jeecg.modules.util;

import javax.crypto.Cipher;
        import javax.crypto.KeyGenerator;
        import javax.crypto.SecretKey;
        import javax.crypto.spec.SecretKeySpec;
        import java.security.KeyPair;
        import java.security.KeyPairGenerator;
        import java.security.PrivateKey;
        import java.security.PublicKey;
        import java.util.Base64;

public class RASUtil {
    public static void main(String[] args) throws Exception {
        // 生成RSA密钥对
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        // 生成AES密钥
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(128); // 可以是128, 192或256位
        SecretKey aesKey = keyGen.generateKey();
        byte[] aesKeyBytes = aesKey.getEncoded();

        // 使用RSA加密AES密钥
        Cipher rsaCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
        rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedAESKey = rsaCipher.doFinal(aesKeyBytes);
        String encryptedAESKeyStr = Base64.getEncoder().encodeToString(encryptedAESKey);
        System.out.println("Encrypted AES Key: " + encryptedAESKeyStr);

        // 使用AES密钥加密身份证号码（示例）
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(aesKeyBytes, "AES"));
        String idNumber = "123456789012345678"; // 示例身份证号码
        byte[] idNumberBytes = idNumber.getBytes();
        byte[] encryptedIdNumber = aesCipher.doFinal(idNumberBytes);
        String encryptedIdNumberStr = Base64.getEncoder().encodeToString(encryptedIdNumber);
        System.out.println("Encrypted ID Number: " + encryptedIdNumberStr);
    }
}
